{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# jobshop_ft06_sat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/examples/jobshop_ft06_sat.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/python/jobshop_ft06_sat.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "This model implements a simple jobshop named ft06.\n",
    "\n",
    "A jobshop is a standard scheduling problem when you must sequence a\n",
    "series of task_types on a set of machines. Each job contains one task_type per\n",
    "machine. The order of execution and the length of each job on each\n",
    "machine is task_type dependent.\n",
    "\n",
    "The objective is to minimize the maximum completion time of all\n",
    "jobs. This is called the makespan.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import collections\n",
    "\n",
    "from ortools.sat.colab import visualization\n",
    "from ortools.sat.python import cp_model\n",
    "\n",
    "\n",
    "def jobshop_ft06() -> None:\n",
    "    \"\"\"Solves the ft06 jobshop.\"\"\"\n",
    "    # Creates the solver.\n",
    "    model = cp_model.CpModel()\n",
    "\n",
    "    machines_count = 6\n",
    "    jobs_count = 6\n",
    "    all_machines = range(0, machines_count)\n",
    "    all_jobs = range(0, jobs_count)\n",
    "\n",
    "    durations = [\n",
    "        [1, 3, 6, 7, 3, 6],\n",
    "        [8, 5, 10, 10, 10, 4],\n",
    "        [5, 4, 8, 9, 1, 7],\n",
    "        [5, 5, 5, 3, 8, 9],\n",
    "        [9, 3, 5, 4, 3, 1],\n",
    "        [3, 3, 9, 10, 4, 1],\n",
    "    ]\n",
    "\n",
    "    machines = [\n",
    "        [2, 0, 1, 3, 5, 4],\n",
    "        [1, 2, 4, 5, 0, 3],\n",
    "        [2, 3, 5, 0, 1, 4],\n",
    "        [1, 0, 2, 3, 4, 5],\n",
    "        [2, 1, 4, 5, 0, 3],\n",
    "        [1, 3, 5, 0, 4, 2],\n",
    "    ]\n",
    "\n",
    "    # Computes horizon dynamically.\n",
    "    horizon = sum([sum(durations[i]) for i in all_jobs])\n",
    "\n",
    "    task_type = collections.namedtuple(\"task_type\", \"start end interval\")\n",
    "\n",
    "    # Creates jobs.\n",
    "    all_tasks = {}\n",
    "    for i in all_jobs:\n",
    "        for j in all_machines:\n",
    "            start_var = model.new_int_var(0, horizon, f\"start_{i}_{j}\")\n",
    "            duration = durations[i][j]\n",
    "            end_var = model.new_int_var(0, horizon, f\"end_{i}_{j}\")\n",
    "            interval_var = model.new_interval_var(\n",
    "                start_var, duration, end_var, f\"interval_{i}_{j}\"\n",
    "            )\n",
    "            all_tasks[(i, j)] = task_type(\n",
    "                start=start_var, end=end_var, interval=interval_var\n",
    "            )\n",
    "\n",
    "    # Create disjuctive constraints.\n",
    "    machine_to_jobs = {}\n",
    "    for i in all_machines:\n",
    "        machines_jobs = []\n",
    "        for j in all_jobs:\n",
    "            for k in all_machines:\n",
    "                if machines[j][k] == i:\n",
    "                    machines_jobs.append(all_tasks[(j, k)].interval)\n",
    "        machine_to_jobs[i] = machines_jobs\n",
    "        model.add_no_overlap(machines_jobs)\n",
    "\n",
    "    # Precedences inside a job.\n",
    "    for i in all_jobs:\n",
    "        for j in range(0, machines_count - 1):\n",
    "            model.add(all_tasks[(i, j + 1)].start >= all_tasks[(i, j)].end)\n",
    "\n",
    "    # Makespan objective.\n",
    "    obj_var = model.new_int_var(0, horizon, \"makespan\")\n",
    "    model.add_max_equality(\n",
    "        obj_var, [all_tasks[(i, machines_count - 1)].end for i in all_jobs]\n",
    "    )\n",
    "    model.minimize(obj_var)\n",
    "\n",
    "    # Solve the model.\n",
    "    solver = cp_model.CpSolver()\n",
    "    solver.parameters.log_search_progress = True\n",
    "    status = solver.solve(model)\n",
    "\n",
    "    # Output the solution.\n",
    "    if status == cp_model.OPTIMAL:\n",
    "        if visualization.RunFromIPython():\n",
    "            starts = [\n",
    "                [solver.value(all_tasks[(i, j)][0]) for j in all_machines]\n",
    "                for i in all_jobs\n",
    "            ]\n",
    "            visualization.DisplayJobshop(starts, durations, machines, \"FT06\")\n",
    "        else:\n",
    "            print(f\"Optimal makespan: {solver.objective_value}\")\n",
    "\n",
    "\n",
    "jobshop_ft06()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
